﻿// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using svs.Mobile.BeyondMedia.Controls;
using svs.Mobile.BeyondMedia.FeedCore;
using svs.Mobile.BeyondMedia.Forms;
using svs.Mobile.BeyondMedia.Objects;

namespace svs.Mobile.BeyondMedia.Transitions
{
	internal static class ViewTransitionManager
	{

		public enum TransitionType
		{
			Basic,
			SlideLeft,
			SlideRight,
			FlipLeft,
			FlipRight
		}

		static IView _FromView, _ToView;

		static ViewTransitionSlide _SlideTransition;
		static ViewTransitionBasic _BasicTransition;
		static ViewTransitionFlip _FlipTransition;

		static IViewTransition _CurrentTransition;

		public static IView ToView
		{
			get { return _ToView; }
		}

		public static IView FromView
		{
			get { return _FromView; }
		}

		static ViewTransitionManager()
		{
			_SlideTransition = new ViewTransitionSlide();
			_BasicTransition = new ViewTransitionBasic();
			_FlipTransition = new ViewTransitionFlip();
		}

		public static void Execute(IView from, IView to, TransitionType type)
		{
			_FromView = from;
			_ToView = to;

			if (Configuration.EnableScreenTransitions)
			{
				switch (type)
				{
					case TransitionType.Basic:
						_CurrentTransition = _BasicTransition;
						break;

					case TransitionType.SlideLeft:
						_SlideTransition.SlideDirection = TransitionType.SlideLeft;
						_CurrentTransition = _SlideTransition;
						break;

					case TransitionType.SlideRight:
						_SlideTransition.SlideDirection = TransitionType.SlideRight;
						_CurrentTransition = _SlideTransition;
						break;

					case TransitionType.FlipLeft:
						_FlipTransition.FlipDirection = TransitionType.FlipLeft;
						_CurrentTransition = _FlipTransition;
						break;

					case TransitionType.FlipRight:
						_FlipTransition.FlipDirection = TransitionType.FlipRight;
						_CurrentTransition = _FlipTransition;
						break;
				}

				if (!_CurrentTransition.IsTransitionAvailable())
					_CurrentTransition = _BasicTransition;
			}
			else
			{
				_CurrentTransition = _BasicTransition;
			}

			_CurrentTransition.Execute();
		}


		public static TransitionControl TransitionCanvas
		{
			get
			{
				return Master.MasterForm.TransitionControl;
			}
		}

		public static void OnTransitionStart()
		{
			_ToView.OnBeforeActivate();

			_FromView.Visible = false;
			TransitionCanvas.Visible = true;
		}

		public static void OnTransitionEnd()
		{
			TransitionCanvas.Visible = false;
			TransitionCanvas.ReleaseOffscreenBitmap();

			_ToView.Visible = true;
			_ToView.Focus();

			ViewManager.OnViewTransitionCompleted(_FromView, _ToView);

		}
	}
}